﻿using System.Data;
using Microsoft.Data.Sqlite;
using SqlSugar;

namespace Common.DataBaseHelper
{
    public class SqLiteHelper
    {
        /// <summary>
        /// 执行Insert,Delete,Update语句的操作
        /// </summary>
        /// <param name="con">数据库连接对象</param>
        /// <param name="sql">需要执行的sql语句</param>
        /// <param name="comType">sql语句类型</param>
        /// <param name="pms">Sql语句的参数</param>
        /// <returns></returns>
        public static int ExecuteNonQuery(SqliteConnection con, string sql,CommandType comType, params SqliteParameter[] pms)
        {
            using var cmd = new SqliteCommand(sql, con)
            {
                CommandType = comType
            };
            
            if (pms != null)
            {
                cmd.Parameters.AddRange(pms);
            }
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            return cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// 返回单个值
        /// </summary>
        /// <param name="con">数据库连接对象</param>
        /// <param name="sql">需要执行的sql语句</param>
        /// <param name="comType">sql语句类型</param>
        /// <param name="pms">sql语句的参数</param>
        /// <returns></returns>
        public static object ExecuteScalar(SqliteConnection con, string sql, CommandType comType, params SqliteParameter[] pms)
        {
            using var cmd = new SqliteCommand(sql, con)
            {
                CommandType = comType
            };

            if (pms != null)
            {
                cmd.Parameters.AddRange(pms);
            }
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            return cmd.ExecuteScalar();
        }

        /// <summary>
        /// 执行sql语句返回一个DataReader
        /// 当返回DataReader的时候，注意：
        /// 1.Connection不能关闭
        /// 2.DataReader不能关闭
        /// 3.command对象执行ExecuteReader()的时候需要传递一个参数CommandBehavior.CloseConnection
        /// </summary>
        /// <param name="con">数据库连接对象</param>
        /// <param name="sql">需要执行的sql语句</param>
        /// <param name="comType">sql语句类型</param>
        /// <param name="pms">sql语句的参数</param>
        /// <returns></returns>
        public static SqliteDataReader ExecuteReader(SqliteConnection con, string sql, CommandType comType, params SqliteParameter[] pms)
        {
           
            try
            {
                using var cmd = new SqliteCommand(sql, con)
                {
                    CommandType = comType
                };

                if (pms != null)
                {
                    cmd.Parameters.AddRange(pms);
                }

                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                //当调用ExecuteReader()方法的时候，如果传递一个CommandBehavior.CloseConnection参数，则表示将来当用户关闭reader的时候，系统会自动将Connection也关闭掉。
                var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return reader;
            }
            catch
            {
                con.Close();
                con.Dispose();
                throw;
            }

        }

        /// <summary>
        /// 返回DataTable
        /// </summary>
        /// <param name="con">数据库连接对象</param>
        /// <param name="sql">需要执行的一个sql语句</param>
        /// <param name="comType">sql语句类型</param>
        /// <param name="pms">sql语句的参数</param>
        /// <returns></returns>
        public static DataTable ExecuteDataTable(SqliteConnection con, string sql, CommandType comType, params SqliteParameter[] pms)
        {
            var sqlAdapter = new SqliteDataAdapter(sql, con)
            {
                SelectCommand = { CommandType = comType }
            }; 
            if (pms != null)
            {
                sqlAdapter.SelectCommand.Parameters.AddRange(pms);
            }
            var dateTable = new DataTable();
            sqlAdapter.Fill(dateTable);
            return dateTable;
        }

        /// <summary>
        /// DataSet
        /// </summary>
        /// <param name="con">数据库连接对象</param>
        /// <param name="sql">需要执行的一个sql语句</param>
        /// <param name="comType">sql语句类型</param>
        /// <param name="pms">sql语句的参数</param>
        /// <returns></returns>
        public static DataSet ExecuteDataSet(SqliteConnection con, string sql, CommandType comType, params SqliteParameter[] pms)
        {
            var sqlAdapter = new SqliteDataAdapter(sql, con)
            {
                SelectCommand = {CommandType = comType}
            };
            if (pms != null)
            {
                sqlAdapter.SelectCommand.Parameters.AddRange(pms);
            }
            var dataSet = new DataSet();
            sqlAdapter.Fill(dataSet);
            return dataSet;
        }

        
    }
}